package com.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.OrderMapper;
import com.pojo.Order;
import com.pojo.OrderProject;
import com.service.OrderProjectService;
import com.service.OrderService;
import com.util.UUIDutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderProjectService orderProjectService;

    /**
     * 将项目表，项目订单中间表，以及订单表全部封装到订单表中
     * @param pageNum
     * @param ispay
     * @param userId
     * @return
     */
    @Override
    public PageInfo<Order> selectAllOrder(int pageNum,String ispay, long userId) {
        PageHelper.startPage(pageNum,3);
        List<Order> orders = orderMapper.selectAllOrder(ispay, userId);
        for (int i = 0; i < orders.size(); i++) {
            OrderProject orderProject = orderProjectService.selectOrderProject(orders.get(i).getId());
            orders.get(i).setOrderProject(orderProject);
        }
        PageInfo pageInfo=new PageInfo(orders);
        return pageInfo;
    }

    /**
     * 生成订单
     * @param order
     * @return
     */
    @Override
    public int insertOrder(Order order) {
        order.setSupportDate(new Date());
        order.setOrderNum(UUIDutil.uuid());
        order.setIspay(0);
        int i = orderMapper.insertOrder(order);
        return i;
    }

    /**
     * 删除订单的同时删除订单项目呀
     * @param orderId
     * @return
     */
    @Override
    public int deleteOrderProject(long orderId) {
        int i = orderProjectService.deleteOrderProject(orderId);
        int i1 = orderMapper.deleteOrder(orderId);
        return i;
    }

    @Override
    public int startOrder(Order order) {
        return orderMapper.startOrder(order);
    }

    @Override
    public int updataOrderStatus(long id, String payOrderNum) {
        return orderMapper.updataOrderStatus(id,payOrderNum);
    }

    @Override
    public Order selectOrderById(long id) {
        return orderMapper.selectOrderById(id);
    }
}
